package com.example.yiwei.crm.view.activity.coordinationActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.activity.menuActivity.LoginActivity;

import org.json.JSONException;
import org.json.JSONObject;


public class UpdatePasswordActivity extends AppCompatActivity {
ImageView img_back;
    EditText edit_password1,edit_password2,edit_password3;
    Button btn_commit;
    private SharedPreferences sp;
   boolean boolen;
    String msg;
    String result;
    private TextView txt_name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_password);
        txt_name= (TextView) findViewById(R.id.update_name);
        img_back= (ImageView) findViewById(R.id.update_password_img_back);
        edit_password1= (EditText) findViewById(R.id.update_password1);
        edit_password2= (EditText) findViewById(R.id.update_password2);
        edit_password3= (EditText) findViewById(R.id.update_password3);
        btn_commit= (Button) findViewById(R.id.update_password_btn_commit);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        txt_name.setText(sp.getString("UAC02",null));
        if ("login".equals(getIntent().getStringExtra("login"))){
            img_back.setVisibility(View.GONE);
        }
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (edit_password3.getText().toString().equals(edit_password2.getText().toString())) {
                    if (edit_password1.getText().toString().equals(edit_password2.getText().toString())){
                        Toast.makeText(UpdatePasswordActivity.this, "新密码不能与原密码相同！", Toast.LENGTH_SHORT).show();
                    }else {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                SC sc = new SC();
                                JSONObject object = sc.storage(sp);
                                try {
                                    object.put("OUAC03", edit_password1.getText().toString());
                                    object.put("UAC03", edit_password2.getText().toString());
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                String json = object.toString();
                                result = NetWorkUtils.getCount(baseClass.update_password_url, json);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            JSONObject object1 = new JSONObject(result);
                                            boolen = true;
                                            boolen = object1.getBoolean("ifsuccess");
                                            if (boolen) {
                                                Toast.makeText(UpdatePasswordActivity.this, "密码修改成功！", Toast.LENGTH_SHORT).show();
                                                Intent intent = new Intent(UpdatePasswordActivity.this, LoginActivity.class);
                                                intent.putExtra("tag", "exit");
                                                startActivity(intent);
                                                System.exit(0);

                                            } else {
                                                msg = object1.getString("msg");
                                                Toast.makeText(UpdatePasswordActivity.this, msg, Toast.LENGTH_SHORT).show();
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                    }
                                });

                            }
                        }).start();
                    }
                }else{
                    Toast.makeText(UpdatePasswordActivity.this, "两次输入的新密码不相同，请重新输入！", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}
